<?php
if(basename($_SERVER['SCRIPT_FILENAME']) == 'config_manager.php')
{
	die('Access denied.');
}

$config_file = PUNG_ROOT . '/config.php';

function open_config_file()
{
	global $config_file;
	
	if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
	{
		chmod($config_file, '0444');
	}
	else
	{
		chmod($config_file, '0777');
	}
}

function write_config_file($posted_settings)
{
	global $config_file;
	
	$fh = fopen($config_file, 'w') or die('Unknown error with config manager.');
	
	$complete_config = array(
	'PUNG_ROOT' 		=> PUNG_ROOT,
	'PUNG_URL' 			=> PUNG_URL,
	'DB_PREFIX' 		=> DB_PREFIX,
	'DB_USER' 			=> DB_USER,
	'DB_PASS' 			=> DB_PASS,
	'DB_HOST' 			=> DB_HOST,
	'DB_NAME' 			=> DB_NAME,
	'THEME' 			=> THEME,
	'MODULE' 			=> MODULE,
	'LANGUAGE' 			=> LANGUAGE,
	'PAGETITLE' 		=> addslashes(PAGETITLE),
	'SITENAME' 			=> addslashes(SITENAME),
	'ENCODING' 			=> ENCODING,
	'FOOTER' 			=> addslashes(FOOTER),
	'DEBUG_MODE' 		=> DEBUG_MODE,
	'SLOGAN' 			=> addslashes(SLOGAN),
	'KEYWORDS' 			=> addslashes(KEYWORDS),
	'DESCRIPTION' 		=> addslashes(DESCRIPTION),
	'ADMIN_TIPS' 		=> ADMIN_TIPS,
	'SESSION_ADMIN' 	=> SESSION_ADMIN,
	'PHPCONSOLE' 		=> PHPCONSOLE,
	'ADMIN_USER' 		=> ADMIN_USER,
	'ADMIN_PASS' 		=> ADMIN_PASS
	);
	
	$result = $complete_config;

	foreach($result as $foo => $bar)
	{
		foreach($posted_settings as $kei => $valju)
		{
			if($posted_settings[$kei][0] == $foo)
			{
				$bar = $posted_settings[$kei][1];
				$newvalues[$posted_settings[$kei][0]] = $posted_settings[$kei][1];
			}
			else
			{
				$newvalues[$foo] = $bar;
			}
		}
	
		unset($bar);
	}
	
	$filedata =	'<?php' . "\n";
	fwrite($fh, $filedata);
	
	foreach($newvalues as $key => $value)
	{
		$filedata =	'define(\'' . $key . '\', \'' . $value . '\');' . "\n";
		fwrite($fh, $filedata);
	}
	
	$filedata =	'?>';
	fwrite($fh, $filedata);

	fclose($fh);
}

function close_config_file()
{
	global $config_file;
	
	if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
	{
		chmod($config_file, '0777');
	}
	else
	{
		chmod($config_file, '0444');
	}
}
?>